<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="电影名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="电影名称"></el-input>
    </el-form-item>
    <el-form-item label="电影宣传图" prop="image">
      <el-input v-model="dataForm.image" placeholder="电影宣传图"></el-input>
    </el-form-item>
    <el-form-item label="电影描述" prop="introduction">
      <el-input v-model="dataForm.introduction" placeholder="电影描述"></el-input>
    </el-form-item>
    <el-form-item label="电影类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="电影类型"></el-input>
    </el-form-item>
    <el-form-item label="出品地区" prop="area">
      <el-input v-model="dataForm.area" placeholder="出品地区"></el-input>
    </el-form-item>
    <el-form-item label="电影时长，以分钟为单位" prop="duration">
      <el-input v-model="dataForm.duration" placeholder="电影时长，以分钟为单位"></el-input>
    </el-form-item>
    <el-form-item label="开始放映日期" prop="startDate">
      <el-date-picker v-model="dataForm.startDate" placeholder="开始放映日期"></el-date-picker>
    </el-form-item>
    <el-form-item label="放映结束日期" prop="endDate">
      <el-date-picker v-model="dataForm.endDate" placeholder="放映结束日期"></el-date-picker>
    </el-form-item>
    <el-form-item label="票价" prop="price">
      <el-input v-model="dataForm.price" placeholder="票价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          image: '',
          introduction: '',
          type: '',
          area: '',
          duration: '',
          startDate: '',
          endDate: '',
          price: ''
        },
        dataRule: {
          name: [
            { required: true, message: '电影名称不能为空', trigger: 'blur' }
          ],
          // image: [
          //   { required: true, message: '电影宣传图不能为空', trigger: 'blur' }
          // ],
          // introduction: [
          //   { required: true, message: '电影描述不能为空', trigger: 'blur' }
          // ],
          type: [
            { required: true, message: '电影类型不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '出品地区不能为空', trigger: 'blur' }
          ],
          duration: [
            { required: true, message: '电影时长，以分钟为单位不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '开始放映日期不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '放映结束日期不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '票价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/movie/movie/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.movie.name
                this.dataForm.image = data.movie.image
                this.dataForm.introduction = data.movie.introduction
                this.dataForm.type = data.movie.type
                this.dataForm.area = data.movie.area
                this.dataForm.duration = data.movie.duration
                this.dataForm.startDate = data.movie.startDate
                this.dataForm.endDate = data.movie.endDate
                this.dataForm.price = data.movie.price
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/movie/movie/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'image': this.dataForm.image,
                'introduction': this.dataForm.introduction,
                'type': this.dataForm.type,
                'area': this.dataForm.area,
                'duration': this.dataForm.duration,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'price': this.dataForm.price
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
